<?php

namespace Home\Controller;

class RecordController extends HomeCoreController {
	
    public $Model = null;

    protected function _initialize(){
		
        parent::_initialize();
		
        $this->Model = D('Record');
		
    }
	
	public function index(){
		
		if(IS_AJAX){
			
			$map['uid'] = session(C('AUTH_KEYS'));
				
			$map['mid'] = session('gid');
			
			$map['bid'] =  1;
			
			$page_size = 10;
			
			$count = $this->Model->where($map)->count();
			
			$Page = new \Think\Page($count, $page_size);
			
			$list = $this->Model
				->where($map)
				->order("id desc")
				->limit($Page->firstRow . ',' . $Page->listRows)
				->select();
				
			
			$str = $this->tojson($list, $count);
			
			echo $str;
			
			exit;
			
        }else{
			
			$this->assign("id", $id);
			
            $this->display();
			
        }
		
    }
	
	public function add(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$post_data['staff'] = json_encode($post_data['staff']);
			
			$data = $this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->update($data);
					
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y', 'id'=>$result));
												
				}else{
						
					$error = $this->Model->getError();
						
					echo json_encode(array('info'=>$error, 'status'=>'n'));
						
				}
					
			}else{
					
				$error = $this->Model->getError();
					
				echo json_encode(array('info'=>$error, 'status'=>'n'));
					
			}
												
		}else{
						
			$this->display();
			
		}
		
	}
	
	public function edit(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$map['id'] = $post_data['id'];
			
			/*$map['uid'] = $post_data['uid'];
			
			$map['mid'] = $post_data['mid'];
			
			$map['pid'] = $post_data['pid'];*/
			
			$info = $this->Model->where($map)->find();
			
			if(!empty($post_data['department'])){
			
				$department = explode(",", substr($post_data['department'],0,-1));
										
				$post_data['department'] = json_encode($department);
			
			}
			
			if(!empty($post_data['hint'])){
			
				if(!empty($info['hint'])){
						
					$post_data['hint'] = json_encode($post_data['hint']);
						
					$post_data['hint'] = $info['hint'] . ',' . $post_data['hint'];
						
				}else{
						
					$post_data['hint'] = json_encode($post_data['hint']);
						
				}
				
			}
			
			if(!empty($post_data['report'])){
				
				if(!empty($info['report'])){
					
					$post_data['report'] = json_encode($post_data['report']);
						
					$post_data['report'] = $info['report'] . ',' . $post_data['report'];
						
				}else{
						
					$post_data['report'] = json_encode($post_data['report']);
				
				}
				
			}
			
			$data = $this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->where($map)->save($data);
					
				if($result){

					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
												
				}else{
						
					$error = $this->Model->getError();
						
					echo json_encode(array('info'=>$error, 'status'=>'n'));
						
				}
					
			}else{
					
				$error = $this->Model->getError();
					
				echo json_encode(array('info'=>$error, 'status'=>'n'));
					
			}
												
		}else{
						
			$this->display();
			
		}
		
	}
	
	public function content(){
		
		$info=I('get.');
			
		$info = $this->Model->where(array('id'=>$info['id']))->find();
		
		$staff = json_decode($info['staff'], true);
		
		$this->assign('staff', $staff);	
		
		$this->assign('record', $info);	
		
		$scripts = M('Scripts')->find($info['sid']);
		
		$this->assign('scripts', $scripts);
			
		$department = json_decode($scripts['department']);
			
		$this->assign('department', $department);
		
		$map['sid'] = $info['sid'];
		
		$map['uid'] = $info['uid'];
		
		$map['mid'] = $info['mid'];
		
		$scene = M('Scene')->where($map)->order('tid asc')->select();
		
		foreach($scene as $k=>$v){
		
			$scene[$k]['department'] = json_decode($v['department']);
		
		}
		
		$this->assign('scene', $scene);
		
		$scene = M('Scene')->where($map)->order('tid asc')->select();
		
		$maps['sid'] = $info['sid'];
		
		$maps['uid'] = $info['uid'];
		
		$maps['mid'] = $info['mid'];
		
		$maps['rid'] = $info['id'];
		
		$hint = M('Hint')->where($maps)->select();
		
		foreach($hint as $k=>$v){
				
			$hint[$k]['voice'] = str_replace('./', '', $v['voice']);
				
		}
		
		$this->assign('hint', $hint);
		
		$report = M('Report')->where($maps)->select();
		
		foreach($report as $k=>$v){
				
			$report[$k]['voice'] = str_replace('./', '', $v['voice']);
				
		}
		
		$this->assign('report', $report);
		
        $this->display();
		
	}
	
	public function repair(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$data=$this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->where(array('id'=>$post_data['id']))->update($data);
				
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
				
			}else{
				
                $error = $this->Model->getError();
				
                echo json_encode(array('info'=>$error, 'status'=>'n'));
				
			}
			
		}else{
			
			$info=I('get.');
			
			$info = $this->Model->where(array('id'=>$info['id']))->find();
			
			$this->assign('info', $info);
			
        	$this->display();
			
		}
		
	}
	
	public function del(){
		
		$ids = I('get.ids');
		
        $map['id'] = array('in', $ids);
		
        $res = $this->Model->where($map)->delete();
		
        if($res){
			
            echo json_encode(array('info'=>'删除成功', 'status'=>'y'));
			
        }else{
			
            $error = $this->Model->getError();
			
			echo json_encode(array('info'=>$error, 'status'=>'n'));
			
        }
		
	}
	
	public function builds(){
		
		$id = I('get.id');

		$scripts = M('Scripts')->where(array('id'=>$id))->find();
		
		$html = $this->buildHtml("index.html",'./',"Record:mod");
		
		$this->display();
		
	}
	
	public function build(){
		
		vendor("Tcpdf.tcpdf");
		
		$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
/*$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
*/
// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, 0, PDF_HEADER_TITLE.' 061', '');
//$pdf->SetHeaderData('', 0, '', '技术支持：三叶CENJE');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('stsongstdlight', '', 12);

// add a page
$pdf->AddPage();

$id = I('get.id');

$record = M('Record')->where(array('id'=>$id))->find();

$staff = json_decode($record['staff'], true);

$scripts = M('Scripts')->find($record['sid']);

$department = json_decode($scripts['department']);
			
$scene = M('Scene')->where(array('sid'=>$record['sid']))->order('tid asc')->select();

$hint = M('Hint')->where(array('rid'=>$record['id']))->select();

$report = M('Report')->where(array('rid'=>$record['id']))->select();

$html = '<div style="text-align:center"><h2>'.preg_replace('/[1-9][0-9]*$/', '',$record['title']).'</h2><h2>演练记录</h2></div>' .

'<h1>基本信息</h1>'.

'<h3>演练名称</h3><div>'.preg_replace('/[1-9][0-9]*$/', '',$record['title']).'</div>'.

//'<h3>演练主控官</h3><div>'.$record['management'].'</div>'.
'<h3>演练总指挥</h3><div>'.$record['management'].'</div>'.

'<h3>演练记录人</h3><div>'.$record['recordman'].'</div>'.

//'<h3>演练时间</h3><div>'.date('Y-m-d H:i:s', $record['start_time']).' 至 ' .date('Y-m-d H:i:s', $record['end_time']).'</div>'.
//'<h3>演练时间</h3><div>'.date('Y-m-d H:i:s', $record['end_time']).'</div>'.
'<h3>演练时间</h3><div>'.date('Y-m-d H:i:s', time()).'</div>'.

'<h3>演练地点</h3><div>'.$record['address'].'</div>'.

'<h3>参演部门</h3>';

foreach($department as $k=>$v){
$html .= '<div>'.$v.'</div>';
}

$html .= '<h3>演练范围</h3><div>'.$scripts['range'].'</div>'.

'<h3>灾害介绍</h3><div>'.$scripts['brief'].'</div>'.

'<h3>演练目的</h3><div>'.$scripts['goal'].'</div>'.

'<h3>演练规则</h3><div>'.$scripts['rules'].'</div>'.

'<h1>演练内容</h1>';

foreach($scene as $ks=>$vs){

$ks = $ks + 1;

$vs['department'] = json_decode($vs['department']);

$html .= '<h3>场景'.$ks.'</h3><div>'.$vs['scenename'].'</div>'.

'<h3>场景描述</h3><div>'.$vs['brief'].'</div>'.

'<h3>部门响应</h3>';

foreach($vs['department'] as $a=>$b){
	
$a = $a + 1;

$html .= '<h3>'.$b.'</h3>';
	
foreach($hint as $c=>$d){
	
if(($vs['id'] == $d['hid']) && ($a == $d['did'])){

//$html .= '<h3></h3><div></div>'.'<div>'.$d['hint'].'</div>';	
//$str_p = preg_split('/\][＼u4e00-＼u9fa5]{1,}\[/',$d['hint'],-1,PREG_SPLIT_OFFSET_CAPTURE);
$str_p = explode('[',$d['hint']);
$s=0;
$string = '';
	foreach ($str_p as $k=>$v){
		if ($s){
		   $string .= '<br/><br/>['.$v;
		}else{

		   $string .= '<br/><br/>'.$v;
		
		}
		$s++;
	}

$html .= '<div>'.$string.'</div>';	

}
	
}

}
	
}

//$html .= '<h1>演练总结</h1>';
$html .= '<h1>领导点评</h1>';
$html .= '<div style="line-height:40px;">各位领导、各位代表、各位参演队员：<br />
	今天，我们在这里举行2018年乌海湖水上应急搜救联合演练活动，此次演练得到了乌海市人民政府、相关部门、企业和社会组织的大力支持，通过各参演单位的充分准备、精心组织和密切配合，演练达到了预期效果，取得了圆满成功。在此，我代表自治区交通运输厅对本次演练的组织、参加单位和全体参演人员表示亲切地慰问和衷心地感谢！随着水上交通事业的快速发展，水上交通安全的风险和压力也随之增加，有效应对水上突发事件，全面提高监管和救助水平，切实保障人民生命财产安全，保护水域生态环境，显得十分迫切和重要。这次应急演练，是交通、公安、渔政、水政、环保、消防等部门，旅游公司、航运企业、红十字会、救援队等13个单位联合实战演习，在90分钟内，顺利完成了事故报警、启动应急预案、疏散乘客、船舶消防、水上搜救、医疗救助、环境监测等七个科目，情景逼真、操作精彩，实战性强，成功检验了我们水上交通快速反应和处置能力，充分展示了应急救援和排险能力，以及与相关部门协同作战能力，更体现出了人民生命至上的理念，全面履行海事救助责任，也必将形成全社会重视水上交通安全的良好局面。
在演练取得成功的同时，我们也要清醒地认识到，这次演练毕竟是一次模拟，与实际事故救援还有一定的区别。为此，各地、各有关部门一定要认真总结本次演练的成功经验，查找存在的问题和不足，深化演练成果的转化和应用，特别是要把安全事故情况设想的更复杂一些、救援情况更困难一些，进一步完善应急救援预案，强化事故应急演练，真正建立起一支训练有素、指挥科学、反应迅速、战斗力强的应急救援队伍，保障人民群众生命财产安全，最大限度地减少水上突发事故造成的人员伤亡和经济损失。
	同志们，安全可以演练，生命不能彩排。安全生产工作就是要未雨绸缪、多做准备。希望大家以此次演练为起点，不断提升水上交通安全管理能力，提升应急反应和处置突发事件的能力，提升救援救助和多部门协同配合的能力，打好预防水上交通突发事件保卫战，为自治区生产形势持续好转作出新的更大的贡献！
	最后，再次感谢现场观摩的各位领导、各位代表，以及所有的参演人员和工作人员。下面我宣布，“2018年乌海湖水上应急搜救应急联合演练”到此结束。</div>
	';

foreach($report as $e=>$f){
	
$html .= '<h3>'.$f['department'].'</h3><div>'.$f['hint'].'</div>';

}

$html .= '<h1>演练签到</h1>';

foreach($staff as $g=>$h){
	
$html .= '<h3>'.$h['department'].'</h3>';

foreach($h['staff'] as $i=>$j){
	
$html .= '<div>'.$j.'</div>';
	
}
	
}
$html .= '<h1>演练组织机构</h1>';
$string = '<div>主办单位：自治区交通运输厅</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市人民政府</div>';

$string .= '<div>承办单位：内蒙古自治区地方海事局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市交通运输局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市地方海事局</div>';

$string .= '<div>协办单位：黄河海勃湾水利枢纽管理局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海湖景区联合管理执法办公室</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海湖旅游发展有限责任公司</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市公安局滨河分局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市漁政管理站</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市消防支队</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市环境保护局水质监测站</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市林业公安局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市水政监察支队</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市海达航运有限责任公司</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;移动公司</div>';

$string .= '<div>演练单位：乌海市地方海事局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市公安局滨河分局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市渔政管理站</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市消防支队</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市环境保护局</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;水质盐测站</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海湖旅游发展有责任公</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市海达航运有限责任公司</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市黄河航运有限责任公司</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海市红十字会漠海应急救援队</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;乌海蓝天救援(筹备)</div>';

$html .= $string;

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->Output(date('YmdHis').'.pdf', 'I');

}
	
}
